﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using RealtorsPortal.Entities;

namespace RealtorsPortal.Data_Access_Layer
{
    public class AdministratorDAL
    {
        DataAccessHelper dah = new DataAccessHelper();


        /// <summary>
        /// Check username & password when admin logged
        /// </summary>
        /// <param name="user">username</param>
        /// <param name="pass">password</param>
        /// <returns>
        /// administrator: success
        /// -1: username doesn't exist
        /// -2 : wrong password
        /// </returns>
        /// 

        public string CheckAdminLogin(string user, string pass) {
            string cmd = "select [Password] from Administrators where Username ='"+ user+"'";
            DataTable dt = dah.GetDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                DataRow r = dt.Rows[0];
                if (r["Password"].ToString() == pass)
                {
                    return "Administrator";
                }
                else
                {
                    return "-2"; // wrong password
                }
            }
            else
            {
                return "-1"; //username doesn't exist
            }
        }

        /// <summary>
        /// Get AdminId by Username
        /// </summary>
        /// <param name="user">Username</param>
        /// <returns>User Id</returns>
        public int GetAdminIdByUsername(string user)
        {
            string cmd = "select AdminId from Administrators where Username = '" + user + "'";
            return (int)dah.GetValue(cmd);
        }

        public DataTable GetAdmiNTableByUsername(string user)
        {
            string cmd = "select * from Administrators where Username LIKE'%"+user+"%'";
            return dah.GetDataTable(cmd);
        
        }

        public bool ChangePassword(int userId, string newPass) {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@adminid", userId));
            paraList.Add(new SqlParameter("@newpass", newPass));
            return dah.ExcuteStoredNonQuery("sp_changePassAdmin", paraList) > 0;
        }

        public DataTable GetAdminsDataTable()
        {
            return dah.GetDataTable("select * from Administrators");
        }

        public bool AddAdmin(Administrator a)
        {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@user", a.Username));
            paraList.Add(new SqlParameter("@password", a.Password));
            paraList.Add(new SqlParameter("@email", a.Email));
            paraList.Add(new SqlParameter("@fullname", a.FullName));
            return dah.ExcuteStoredNonQuery("sp_insertAdmin", paraList) > 0;
        }

        public bool EditAdmin(Administrator a)
        {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@adminid", a.AdminId));
            paraList.Add(new SqlParameter("@user", a.Username));
            paraList.Add(new SqlParameter("@email", a.Email));
            paraList.Add(new SqlParameter("@fullname", a.FullName));
            return dah.ExcuteStoredNonQuery("sp_updateAdmin", paraList) > 0;
        }

        public bool DeleteAdmin(int adminId)
        {
            List<SqlParameter> paraList = new List<SqlParameter>();
            paraList.Add(new SqlParameter("@adminid", adminId));
            return dah.ExcuteStoredNonQuery("sp_deleteAdmin", paraList) > 0;
        }
    }
}